//
//  QJMoneyPayVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/3/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMoneyPayVC.h"
//支付宝SDK
#import <AlipaySDK/AlipaySDK.h>
//微信
#import "WXApiManager.h"
@interface QJMoneyPayVC () <WXApiManagerDelegate>
@property (nonatomic, strong) NSString *payType;
@property (nonatomic, strong) UIButton *selectBtn;
@end

@implementation QJMoneyPayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self defaultUIPrepare];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 默认配置
- (void)defaultUIPrepare{
    self.title = @"支付";
    self.yeBtn.selected = YES;
    self.payType = kBalanceMoney;
    self.selectBtn = self.yeBtn;
    self.selectBtn.tag = self.yeBtn.tag;
    
    //支付宝支付成功通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipayGengXingData) name:@"alipaysuccsed" object:nil];
    //支付宝支付失败通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipayErrorAction) name:@"alipayerror" object:nil];
    //微信支付
    [WXApiManager sharedManager].delegate = self;
}
- (IBAction)payWaySelectAction:(UIButton *)sender {
    NSInteger tag = sender.tag;
    if (self.selectBtn.tag == tag) {
        //点击的是相同的按钮
    }else{
        switch (tag) {
            case 200:
            {
                self.yeBtn.selected = YES;
                self.selectBtn.selected = NO;
                self.payType = kBalanceMoney;
                self.selectBtn = self.yeBtn;
                self.selectBtn.tag = self.yeBtn.tag;
                self.selectBtn.selected = YES;
            }
                break;
            case 201:
            {
                self.aliBtn.selected = YES;
                self.selectBtn.selected = NO;
                self.payType = kAliPayMoney;
                self.selectBtn = self.aliBtn;
                self.selectBtn.tag = self.aliBtn.tag;
                self.selectBtn.selected = YES;
            }
                break;
            case 202:
            {
                self.wxBtn.selected = YES;
                self.selectBtn.selected = NO;
                self.payType = kWxpayMoney;
                self.selectBtn = self.wxBtn;
                self.selectBtn.tag = self.wxBtn.tag;
                self.selectBtn.selected = YES;
            }
                break;
                
            default:
                break;
        }
    }
}
- (IBAction)okPayBtnClickAction:(UIButton *)sender {
    WEAKSELF
    NSString *mobileStr = self.mobileTF.text ? self.mobileTF.text : @"";
    NSString *moneyStr = self.moneyTF.text ? self.moneyTF.text : @"";
    if (mobileStr.length > 0 && moneyStr.length > 0 && self.payType.length > 0) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:mobileStr forKey:@"mobile"];
        if ([self.payType containsString:@"payment"]) {
            [dict setObject:moneyStr forKey:@"payAmount"];
        }else{
            [dict setObject:moneyStr forKey:@"amount"];
        }
        [[HttpManager shareHttpManager] postDictWithUrl:self.payType parameter:dict hiddenHUD:NO HUDTitle:@"正在支付..." success:^(NSURLSessionDataTask *operetion, id response) {
            [weakSelf payOrderActionWithPayStr:response];
        } failure:^(NSURLSessionDataTask *operetion, id response) {
            
        }];
    }else if (mobileStr.length < 1){
        [MBPHUD showAlertWithTitle:@"对方账户不能为空!"];
    }else if (moneyStr.length < 1){
        [MBPHUD showAlertWithTitle:@"支付金额不能为空!"];
    }
}
#pragma mark -- 支付方法
- (void)payOrderActionWithPayStr:(id)payStr{
    if ([self.payType containsString:@"aliPay"]) {
        NSString *responseStr = [NSString stringWithFormat:@"%@",payStr[@"response"]];
        //支付宝
        [[AlipaySDK defaultService] payOrder:responseStr fromScheme:MyAppScheme callback:^(NSDictionary *resultDic) {
            
            if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
                //支付成功
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alipaysuccsed" object:resultDic];
            }else{
                //支付失败
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alipayerror" object:resultDic];
            }
            
        }];
    }else if ([self.payType containsString:@"wxpay"]){
        //微信
        //时间戳
        //time_t now;
        //time(&now);
        //NSString *stamp  = [NSString stringWithFormat:@"%ld", now];
        if ([payStr isKindOfClass:[NSDictionary class]]) {
            PayReq *req = [[PayReq alloc]init];
            req.partnerId = [payStr objectForKey:@"partnerid"];
            req.prepayId = [payStr objectForKey:@"prepayid"];
            req.nonceStr = [payStr objectForKey:@"noncestr"];
            NSString *time = [payStr objectForKey:@"timestamp"];
            req.timeStamp = [time intValue];
            req.package = [payStr objectForKey:@"package"];
            req.sign = [payStr objectForKey:@"sign"];
            [WXApi sendReq:req];
        }
    }else{
        //余额
        [MBPHUD showAlertWithTitle:@"余额支付成功!"];
        KPopViewControllerAction;
    }
}
- (void)alipayGengXingData{
    
    [UIView animateWithDuration:3.0f animations:^{
        
        [MBPHUD showAlertWithTitle:@"支付宝支付成功!"];
        
    }completion:^(BOOL finished) {
        
        KPopViewControllerAction;
    }];
    
}
#pragma mark -- 支付宝充值失败的提示
- (void)alipayErrorAction{
    [UIView animateWithDuration:3.0f animations:^{
        
        [MBPHUD showAlertWithTitle:@"支付宝支付失败!"];
        
    }completion:^(BOOL finished) {
        
        KPopViewControllerAction;
    }];
}
#pragma mark -- 微信支付成功回调方法
- (void)managerDidPaySucced{
    
    [UIView animateWithDuration:3.0f animations:^{
        
        [MBPHUD showAlertWithTitle:@"微信支付成功!"];
        
    }completion:^(BOOL finished) {
        
        KPopViewControllerAction;
    }];
}
#pragma mark -- 微信支付失败回调方法
- (void)managerDidPayFailed{
    
    
    [UIView animateWithDuration:3.0f animations:^{
        
        [MBPHUD showAlertWithTitle:@"微信支付失败!"];
        
    }completion:^(BOOL finished) {
        
        KPopViewControllerAction;
    }];
    
    
}


@end
